package com.example.mkio.androidpractice;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class WeiBoActivity extends AppCompatActivity {


    private ViewPager mViewPager;
    private ImageView mImageView;
    private TextView mTextView1, mTextView2, mTextView3, mTextView4;
    private List<View> mViews;
    private int offset;
    private int currentIndex;
    private int bmWidth;
    private View mView1, mView2, mView3, mView4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_bo);
        InitImageView();
        InitTextView();
        InitViewPager();
    }

    private void InitViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.vPager);
        mViews = new ArrayList<View>();
//        view = LayoutInflater.from(context).inflate(R.layout.dialog_change_province_pop, null);
//
        LayoutInflater inflater = getLayoutInflater();
        mView1 = inflater.inflate(R.layout.one, null);
        mView2 = inflater.inflate(R.layout.two, null);
        mView3 = inflater.inflate(R.layout.three, null);
//        mView4 = inflater.inflate(R.layout.four, null);
        mViews.add(mView1);
        mViews.add(mView3);
        mViews.add(mView2);
//        mViews.add(mView4);
        mViewPager.setAdapter(new MyViewAdapter(mViews));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new MyOnPagerListener());
    }

    private void InitTextView() {

        mTextView1 = (TextView) findViewById(R.id.text1);
        mTextView2 = (TextView) findViewById(R.id.text2);
        mTextView3 = (TextView) findViewById(R.id.text3);

        mTextView1.setOnClickListener(new MyOnClickListener(0));
        mTextView2.setOnClickListener(new MyOnClickListener(1));
        mTextView3.setOnClickListener(new MyOnClickListener(2));
    }

    private void InitImageView() {
        mImageView = (ImageView) findViewById(R.id.cursor);
        bmWidth = BitmapFactory.decodeResource(getResources(), R.drawable.iv_add_pic).getWidth();
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        int screenW = mDisplayMetrics.widthPixels;//获取屏幕分辨率
        offset = (screenW / mViews.size() - bmWidth) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        mImageView.setImageMatrix(matrix);// 设置动画初始位置

    }

    private class MyOnClickListener implements View.OnClickListener {
        private int index;

        public MyOnClickListener(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    private class MyViewAdapter extends PagerAdapter {
        private List<View> mListViews;

        public MyViewAdapter(List<View> views) {
            mListViews = views;
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(mListViews.get(position));
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }
    }

    private class MyOnPagerListener implements ViewPager.OnPageChangeListener {
        int one = offset * 2 + bmWidth;// 页卡1 -> 页卡2 偏移量
        int two = one * 2;// 页卡1 -> 页卡3 偏移量

        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }



        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currentIndex == 1) {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    } else if (currentIndex == 2) {
                        animation = new TranslateAnimation(two, 0, 0, 0);
                    }
                    break;
                case 1:
                    if (currentIndex == 0) {
                        animation = new TranslateAnimation(offset, one, 0, 0);
                    } else if (currentIndex == 2) {
                        animation = new TranslateAnimation(two, one, 0, 0);
                    }
                    break;
                case 2:
                    if (currentIndex == 0) {
                        animation = new TranslateAnimation(offset, two, 0, 0);
                    } else if (currentIndex == 1) {
                        animation = new TranslateAnimation(one, two, 0, 0);
                    }
                    break;
            }
//            Animation animation = new TranslateAnimation(one * currentIndex, one * arg0, 0, 0);//显然这个比较简洁，只有一行代码。
            currentIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            mImageView.startAnimation(animation);
//            Toast.makeText(WeiBoActivity.this, "您选择了" + mViewPager.getCurrentItem() + "页卡", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }
}
